package com.deyuanyun.pic.ctrl.controller.vo.defect;

import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.ctrl.controller.vo.DefaultCommand;
import com.deyuanyun.pic.ctrl.domain.CtrlRepairTask;
import com.deyuanyun.pic.pbf.domain.DefectStatistics;
import com.deyuanyun.pic.pbf.enumeration.RepairStateEnum;
import com.deyuanyun.pic.settings.controller.pipe.vo.PipeLocationDTO;
import com.deyuanyun.pic.settings.service.pipe.PipeLocationService;
import com.deyuanyun.pic.settings.support.biz.PipeLineUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 缺陷信息统计 数据传输对象
 *
 * @author huanglw
 * @date 2016-7-18
 *
 * History: [修改人][时间][修改描述]
 */
public class DefectStatisticsVO extends DefaultCommand<CtrlRepairTask> {

    // 项目名称
    private String repairTaskName;
    // 管道名称
    private String pipeLineName;
    // 起点编号
    private String fromPointNum;
    // 终点编号
    private String toPointNum;
    //  建议开工时间
    private String planStartDate;
    // 建议竣工时间
    private String planCompletionDate;
    // 施工单位
    private String assemblyCompany;
    // 缺陷信息统计
    private DefectInfoStatistics defectStatistics;
    // 维修状态数量统计
    private List<RepairStateStatistics> repairStatisticsList;

    public String getRepairTaskName() {
        return repairTaskName;
    }

    public void setRepairTaskName(String repairTaskName) {
        this.repairTaskName = repairTaskName;
    }

    public String getPipeLineName() {
        return pipeLineName;
    }

    public void setPipeLineName(String pipeLineName) {
        this.pipeLineName = pipeLineName;
    }

    public String getFromPointNum() {
        return fromPointNum;
    }

    public void setFromPointNum(String fromPointNum) {
        this.fromPointNum = fromPointNum;
    }

    public String getToPointNum() {
        return toPointNum;
    }

    public void setToPointNum(String toPointNum) {
        this.toPointNum = toPointNum;
    }

    public String getPlanStartDate() {
        return planStartDate;
    }

    public void setPlanStartDate(String planStartDate) {
        this.planStartDate = planStartDate;
    }

    public String getPlanCompletionDate() {
        return planCompletionDate;
    }

    public void setPlanCompletionDate(String planCompletionDate) {
        this.planCompletionDate = planCompletionDate;
    }

    public String getAssemblyCompany() {
        return assemblyCompany;
    }

    public void setAssemblyCompany(String assemblyCompany) {
        this.assemblyCompany = assemblyCompany;
    }

    public DefectInfoStatistics getDefectStatistics() {
        return defectStatistics;
    }

    public void setDefectStatistics(DefectInfoStatistics defectStatistics) {
        this.defectStatistics = defectStatistics;
    }

    public List<RepairStateStatistics> getRepairStatisticsList() {
        return repairStatisticsList;
    }

    public void setRepairStatisticsList(List<RepairStateStatistics> repairStatisticsList) {
        this.repairStatisticsList = repairStatisticsList;
    }

    public void fromDomain(CtrlRepairTask domain) {
        if (ObjectUtil.isEmptyAll(domain)) {
            return;
        }
        // 项目名称
        this.repairTaskName = domain.getName();
        // 管道名称
        this.pipeLineName = PipeLineUtil.getPipeLineName(domain.getPipeLineId());

        PipeLocationService pipeLocationService = (PipeLocationService)SpringAppliction.getBean(PipeLocationService.class);
        PipeLocationDTO fromPipe = null;
        if (StringUtils.isNotNullEmpty(domain.getFromPointId())) {
            fromPipe = pipeLocationService.queryPipeLocationById(domain.getPipeLineId(), domain.getFromPointId());
        }
        PipeLocationDTO toPipe = null;
        if (StringUtils.isNotNullEmpty(domain.getToPointId())) {
            toPipe = pipeLocationService.queryPipeLocationById(domain.getPipeLineId(), domain.getToPointId());
        }
        // 起点编号
        this.fromPointNum = fromPipe==null ? null : fromPipe.getName();
        // 终点编号
        this.toPointNum = toPipe==null ? null : toPipe.getName();
        // 建议开工时间
        this.planStartDate = StringUtils.convertDateToString(domain.getPlanStartDate(), StringUtils.DATE);
        // 建议竣工时间
        this.planCompletionDate = StringUtils.convertDateToString(domain.getPlanCompletionDate(), StringUtils.DATE);
        // 施工单位
        this.assemblyCompany = domain.getAssemblyCompany();
    }

    public void fromDefectStatistics(DefectStatistics defectStatistics) {

        DefectInfoStatistics defectInfoStatistics = new DefectInfoStatistics();
        defectInfoStatistics.fromDomain(defectStatistics);

        List<RepairStateStatistics> repairStateStatisticses = new ArrayList<RepairStateStatistics>(4);
        repairStateStatisticses.add(new RepairStateStatistics(RepairStateEnum.ALREADY_REPAIR.getLabel(), Long.parseLong(defectStatistics.getAlreadyRepair())));
        repairStateStatisticses.add(new RepairStateStatistics(RepairStateEnum.WAIT_FOR_REPAIR.getLabel(), Long.parseLong(defectStatistics.getWaitForRepair())));
        repairStateStatisticses.add(new RepairStateStatistics(RepairStateEnum.MAINTENANCE.getLabel(), Long.parseLong(defectStatistics.getMaintenance())));
        repairStateStatisticses.add(new RepairStateStatistics(RepairStateEnum.DIFFICULT_TO_REPAIR.getLabel(), Long.parseLong(defectStatistics.getDifficultToRepair())));

        this.defectStatistics = defectInfoStatistics;
        this.repairStatisticsList = repairStateStatisticses;
    }


    class RepairStateStatistics {

        private String name;
        public Long val;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Long getVal() {
            return val;
        }

        public void setVal(Long val) {
            this.val = val;
        }

        public RepairStateStatistics( String name, Long val) {
            this.name = name;
            this.val = val;
        }
    }

    /**
     * 缺陷信息统计
     */
    class DefectInfoStatistics {

        // 计划维修个数
        private String planRepair;
        // 已维修个数
        private String alreadyRepair;
        // 待维修个数
        private String waitForRepair;
        // 正在维修个数
        private String maintenance;
        // 难以维修个数
        private String difficultToRepair;

        public String getPlanRepair() {
            return planRepair;
        }

        public void setPlanRepair(String planRepair) {
            this.planRepair = planRepair;
        }

        public String getAlreadyRepair() {
            return alreadyRepair;
        }

        public void setAlreadyRepair(String alreadyRepair) {
            this.alreadyRepair = alreadyRepair;
        }

        public String getWaitForRepair() {
            return waitForRepair;
        }

        public void setWaitForRepair(String waitForRepair) {
            this.waitForRepair = waitForRepair;
        }

        public String getMaintenance() {
            return maintenance;
        }

        public void setMaintenance(String maintenance) {
            this.maintenance = maintenance;
        }

        public String getDifficultToRepair() {
            return difficultToRepair;
        }

        public void setDifficultToRepair(String difficultToRepair) {
            this.difficultToRepair = difficultToRepair;
        }

        public void fromDomain(DefectStatistics domain) {
            // 计划维修个数
            this.planRepair = domain.getPlanRepair();
            // 已维修个数
            this.alreadyRepair = domain.getAlreadyRepair();
            // 待维修个数
           this.waitForRepair = domain.getWaitForRepair();
            // 正在维修个数
            this.maintenance = domain.getMaintenance();
            // 难以维修个数
            this.difficultToRepair = domain.getDifficultToRepair();
        }
    }
}
